<?php

namespace app\index\controller;

use think\Db;
use Wechat\Wechat;

class Accept
{

	public function index()
	{
		file_put_contents('./../runtime/wechat.txt', date('Y-m-d H:i:s') . "\n ");
		$weixinconfig = config('wechat');
		$weixin = new Wechat($weixinconfig);
		$weixin->valid();
		$type = $weixin->getRev()->getRevType(); // 消息类型
		$wxid = $weixin->getRev()->getRevFrom(); // 用户openid
		file_put_contents('./../runtime/wechat.txt', $wxid . "\n ", FILE_APPEND);
		$reMsg = "";
		$event = ['event' => ''];
		file_put_contents('./../runtime/wechat.txt', $type . "\n ", FILE_APPEND);
		switch ($type)
		{
			case 'text':
				$content = $weixin->getRev()->getRevContent();
				file_put_contents('./../runtime/wechat.txt', $content . "\n ", FILE_APPEND);
				break;
			case 'event':
				$event = $weixin->getRev()->getRevEvent();
				$content = json_encode($event);
				break;
			case 'image':
				$content = json_encode($weixin->getRev()->getRevPic());
				$reMsg = "图片很美！";
				break;
			case 'location':
				$content = json_encode($weixin->getRev()->getRevGeo());
				// $reMsg = "您所在的位置很安全！";
				break;
			default:
				$content = '';
				$reMsg = '未知消息类型';
		}
		file_put_contents('./../runtime/wechat.txt', $reMsg . "--reMsg \n ", FILE_APPEND);
		if ($reMsg)
		{
			file_put_contents('./../runtime/wechat.txt', "reMsg \n ", FILE_APPEND);
			echo $weixin->text($reMsg)->reply();
			exit();
		}
		
		if ($event['event'] == "subscribe")
		{ // 用户关注
			$data = db('scan_log')->where('openid', $wxid)->order('id desc')->find();
			if ($data) {
                $newsData = array([
                    'Title' => '注册会员',
                    'Description' => '有推荐人推荐您开启定制生活！！',
                    'PicUrl' => 'http://' . $_SERVER['HTTP_HOST'] . PUBLIC_URL . 'static/home/images/wechat_cover.jpg',
                    'Url' => request()->domain() . url('common/team_scan_code', ['parent_openid' => $data['p_openid']])
                ]);
                echo $weixin->news($newsData)->reply();
                exit();
			}
            $newsData = array([
                'Title' => '欢迎关注衣依不舍',
                'Description' => '服装定制网络平台！！',
                'PicUrl' => 'http://' . $_SERVER['HTTP_HOST'] . PUBLIC_URL . 'static/home/images/wechat_cover.jpg',
                'Url' => request()->domain() . url('index/index')
            ]);
            echo $weixin->news($newsData)->reply();
			exit();
		}
		file_put_contents('./../runtime/wechat.txt', "--unsubscribe \n ", FILE_APPEND);
		if ($event['event'] == "unsubscribe")
		{ // 取消关注
			file_put_contents('./../runtime/wechat.txt', "--unsubscribe-- \n ", FILE_APPEND);
			exit();
		}
		
		file_put_contents('./../runtime/wechat.txt', json_encode($event) . "--event \n ", FILE_APPEND);
		// 判断用户是否点击的菜单
		if ($event['event'] == "CLICK")
		{
			file_put_contents('./../runtime/wechat.txt', 'aaa' . "\n ", FILE_APPEND);
			$content = $event['key'];
			switch ($content)
			{
				case "best":
				case "new":
				case "hot":
					$newsData = array();
					echo $weixin->news($newsData)->reply();
					exit();
					break;
				case "ddcx":
					$text = '';
					echo $weixin->text($text)->reply();
					exit();
					break;
				case "jcbd":
					
					break;
				case "info":
					echo $weixin->text('')->reply();
					exit();
					break;
				case "qd":
					break;
				case 'qdcx':
					break;
				default:
					echo $weixin->text("未定义菜单事件{$content}")->reply();
					exit();
					break;
			}
		}
		// $content = $api->getstr($content);
		// 处理用户扫一扫
		file_put_contents('./../runtime/wechat.txt', 'SCAN' . "\n ", FILE_APPEND);
		if ($event['event'] == "SCAN")
		{
			file_put_contents('./../runtime/wechat.txt', 'bbb' . "\n ", FILE_APPEND);
			$content = intval($event['key']); // 场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000
			
			echo $weixin->text('扫码业务处理')->reply();
			exit();
		}
		
		file_put_contents('./../runtime/wechat.txt', $content . "\n ", FILE_APPEND);
		file_put_contents('./../runtime/wechat.txt', date('Y-m-d H:i:s') . "\n ", FILE_APPEND);
		// 处理用户的输入
		if ($content)
		{
			if ($content == '1')
			{
				echo $weixin->text('你说的有道理啊！！！')->reply();
				exit();
			}
            if ($content == '酒')
            {
                $newsData = array([
                    'Title' => '湖南酒业协会',
                    'Description' => '服装定制网络平台！！',
                    'PicUrl' => request()->domain() . DS . 'static/home/images/zpzj.jpg',
                    'Url' => request()->domain() . url('index/index')
                ]);
                echo $weixin->news($newsData)->reply();
                exit();
            }
			if ($content == '客服')
			{
				echo $weixin->text('客服mm来了')->reply();
				exit();
			}
		}
	}
}
